import axios from 'axios';
import { getToken } from '@/utils/auth';

const exportTableData = async (url, filename = 'export') => {
  try {
    const response = await axios.get(url,
      {
        headers: {
          Authorization: getToken(),
        },
        responseType: 'blob'
      }
    );

    // 检查响应数据
    if (response.status === 200) {
      const data = response.data;
      const blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=UTF-8' });
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = filename + ".xls";
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  } catch (error) {
    console.error('导出数据时出错:', error);
  }
};

export default exportTableData;
